package com.awesome.javademo.annotation.proxy.proxy4;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.awesome.common.util.log.ShowLogUtil;
import com.awesome.javademo.R;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proxy);
        XiaoMin xiaoMin = proxy(XiaoMin.class);
        xiaoMin.getIp();
        xiaoMin.getIpMsg(new PersonBean(99));
    }

    private <T> T proxy(Class<T> clazz) {
        /**
         * @param ClassLoader 反射类的ClassLoader
         * @param Class<?>[]  最终的返回类型，一般为ClassLoader或他的父类
         * @param InvocationHandler
         * @return
         */
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new InvocationHandler() {
            /**
             * @param proxy
             * @param method 调用了反射类的某个方法
             * @param args   调用了反射类的某个方法的参数，对象数组形式
             * @return
             * @throws Throwable
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 调用被代理类对象的方法
                URL url = method.getAnnotation(URL.class);
                ShowLogUtil.info(" url.value()=" + url.value());
                return null;
            }
        });
    }
}